#include "keynum.h"

KeyNum::KeyNum(QWidget *parent) : QWidget(parent)
{


    this->setMinimumSize(500,180);
    this->setMaximumSize(500,180);
    this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏


    label_pic_bg = new QLabel(this);
    label_pic_bg->setPixmap(QPixmap(":/images/line.png"));
    label_pic_bg->setGeometry(50,55,400,2);
//    label_pic_bg->setScaledContents(true);

    item = new ShowLabel(this);
    item->addFontSize(-1);
    item->setAlignment(Qt::AlignCenter);
    item->setGeometry(QRect(50,10,200,50));
    item->setText(tr("设置: "));

    display = new ShowLabel(this);
    display->addFontSize(1);
    display->setAlignment(Qt::AlignCenter);
    display->setGeometry(QRect(250,10,200,50));
    display->setText(tr(""));


    for (int i = 0; i < 10; ++i) {
        int x = 20,y = 0;
        if(i<5){
            y = 75;
            x = x + i*80;
        }else{
            y = 130;
            x = x + (i-5)*80;
        }
        digitButtons[i] = createButton(x,y,QString::number(i), SLOT(digitClicked()));
    }

    keyB = createButton(420,75,tr("B"), SLOT(backspaceClicked()));
    keyC = createButton(420,130,tr("C"), SLOT(cleanClicked()));

}

void KeyNum::setItem(QString itemName)
{
    item->setText(itemName);
}

void KeyNum::showRet(QString ret)
{
    display->setText(ret);
}

QString KeyNum::getValue()
{
    return display->text();
}

void KeyNum::digitClicked()
{
    //获取发送者对象指针
    Button *clickedButton = qobject_cast<Button *>(sender());
    QString digitValue = clickedButton->getId();//按键的数值

    display->setText(display->text() + digitValue);
}



void KeyNum::backspaceClicked()
{

    QString text = display->text();
    text.chop(1);
    if (text.isEmpty()) {
        text = "";
    }
    display->setText(text);
}

void KeyNum::cleanClicked()
{
    display->setText(tr(""));
}




Button *KeyNum::createButton(int x, int y,const QString &num, const char *member)
{

    Button *button = new Button(this);
    QString pixName1 = QString(tr(":/images/numkey/")+num+tr("_1.png"));
    QString pixName2 = QString(tr(":/images/numkey/")+num+tr("_2.png"));

    qDebug() << pixName1 << pixName2 << "x=" << x << "y=" << y;

    button->setId(num);
    button->setButtonPicture(pixName1);
    button->setPressPicture(pixName2);
    button->setReleasePicture(pixName1);
    button->setEnterPicture(pixName2);
    button->setReleasePicture(pixName1);
    button->set_X_Y_width_height(x,y,60,50);

    connect(button, SIGNAL(clicked()), this, member);
    return button;
}
